import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Button  } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import Footer from '../index/footer'
import './grouplist.scss'





export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      reslut: [],
      updatete: true,
      mytime: null,
      qid:""
    }
  }
  config = {
    navigationBarTitleText:'我加入的社群'
  }

  componentWillMount () {
  	/*
  		yy 分割符 为顾问小程序二维码
  		aa 分隔符  为 活动分享到朋友圈的二维码
  		cccccc 分隔符 为 公众号推送消息然后点击进入 顾问聊天
  		introducer 分割符  app分享小程序二维码 推荐注册
  	*/
    var par = this.$router.params
    var cid = par.scene
		if(cid&&cid.indexOf("yy")>=0){
			var arr=cid.split("yy");
    	var dateinfo = Taro.getStorageSync('dateinfo');
      dateinfo.qid = arr[0]
      dateinfo.counselorId = arr[1]
      Taro.setStorage({key:'dateinfo',data:dateinfo})
      CommonJs.openTo('/pages/login/login')
		}
		else if(cid&&cid.indexOf("introducer")>=0){
			var arr=cid.split("introducer");
    	var dateinfo = Taro.getStorageSync('dateinfo');
    	dateinfo.introducer = arr[0];
    	dateinfo.qid="";
      Taro.setStorage({key:'dateinfo',data:dateinfo})
      CommonJs.openTo('/pages/login/login')
		}
    else if(cid&&cid.indexOf("yy")<0&&cid.indexOf("introducer")<0) {
    	this.$preload({
      	params:cid
    	})
      CommonJs.open('/pages/activity/activity?scene='+cid);
    }
    else{
    	const accountInfo = Taro.getAccountInfoSync();
			const appid=accountInfo.miniProgram.appId;
			var url='/advert/papi/v1.9/apply-setting/identification/'+appid
			var _this=this;
			CommonJs.ajaxRequest(url,'','GET',function(res){
				if(res.data){
					CommonJs.open('/pages/shop/index');
				}
				else{
					_this.getdate();
				}
			})
    	
    }
  }
  componentDidShow () {
  	this.getdate()

  }
  componentDidUpdate () {

    var _this = this
    var updatete = _this.state.updatete
    if (updatete) {
      _this.setState({
        updatete: false
      })
      // _this.padntoz()
      _this.mytime = setTimeout(() => { _this.padntoz() }, 600)
    }
  }

  open(url,type){
    this.$preload({
      type:type
    })
    CommonJs.open(url);
  }



  padntoz(){
    var _this=this
    var par = _this.$router.params
    // console.log(JSON.stringify(par))
    if (par.activityId !== undefined) {
      _this.$preload({
        activityId: par.activityId
      })
      CommonJs.openTo('/pages/activitydetails/activitydetails')
    }
  }
  getdate(){
    var _this=this
    var date = {

    }
    CommonJs.ajaxRequest('/cluster/myClustersall',date,'GET',function(res){
      // console.log(JSON.stringify(res)+'55555555555555555555')
      _this.setState({
        reslut: res.content
      })
    })
  }
  getnav(id, types){
    var _this = this;
    _this.$preload({
      type:1
    })
    var dateinfo=Taro.getStorageSync('dateinfo')
    dateinfo.qid=id
    Taro.setStorage({key:'dateinfo',data:dateinfo})
    if (types === 1) {
      Taro.showToast({
      	title:'加载中',
      	icon: 'none',
      	duration: 1000
      })
      var dates = {
        clusterId:id
      }
      CommonJs.ajaxRequest('/clusterShop/getClusterShopByclusterId',dates,'GET',function(res){
      	if(res.content&&res.content.shopId){
      		Taro.setStorage({key:'shopId',data:res.content.shopId})
        	Taro.setStorage({key:'dealerId',data:res.content.dealerId})
         
      	}
      	else{
      		Taro.setStorage({key:'shopId',data:''})
      		Taro.setStorage({key:'dealerId',data:''})
      	}
      	_this.opendetails('/pages/activity/activity?scene='+id)
      })
    } else if (types === 2) {
      var date1={
        clusterId:id
      }
      Taro.showToast({
        title:'加群中',
        icon: 'none',
        duration: 1000
      })
      setTimeout(() => {
        CommonJs.ajaxRequest('/openClusterBindingUser/create',date1,'GET',function(res){
          CommonJs.open('/pages/activity/activity?scene='+id);
        })
      }, 1000)
    }
  }
  saoma(){
		this.getnav('976',1)
    var _this = this
    Taro.scanCode({
      success(res) {
      	//console.log(JSON.stringify(res)+"//dddddd")
        let url = res.path
        let id = url.split("=")[1]
        var cid = id
				if(cid&&cid.indexOf("yy")>=0){
					var arr=cid.split("yy");
					var dateinfo = Taro.getStorageSync('dateinfo');
		    	if (dateinfo) {
		          dateinfo.qid = arr[0]
		          dateinfo.counselorId = arr[1]
		      }
		    	else {
		          dateinfo = {
		            qid:arr[0],
		            counselorId:arr[1]
		          }
		      }
		      Taro.setStorage({key:'dateinfo',data:dateinfo})
			  	var curl="/crmweb/papi/v1.9/customer-shop/crate/customerShop?consultantId="+arr[1]
			  	CommonJs.ajaxRequest(curl,'','POST',function(res){
			  		if(res.data!=null){
			  			_this.opendetails('/pages/index/changegw?cid='+arr[1]+'&qid='+arr[0])
			  		}
			  		else{
			  			setTimeout(function(){
					  		Taro.showToast({
					        title:'成功绑定，界面跳转中..',
					        icon: 'none',
					        duration: 2000
					      });
					    },300)
				      setTimeout(function(){
				      	_this.getnav(arr[0],2)
				      },2000)
				    }
			  	},"wfw")
				}
				else if(cid&&cid.indexOf("introducer")>=0){
					var arr=cid.split("introducer");
		    	var dateinfo = Taro.getStorageSync('dateinfo');
		    	dateinfo.introducer = arr[0];
		    	dateinfo.qid="";
		      Taro.setStorage({key:'dateinfo',data:dateinfo})
		      CommonJs.openTo('/pages/login/login')
				}
        else{
        	_this.getnav(id,2)
        }
      }
    })
  }
  opendetails(url){
    CommonJs.openTo(url);
  }

  render () {
    const resy = this.state.reslut
    let htmls = null
    if (resy.length > 0) {
      htmls = (
        <View></View>
      )
    } else {
      htmls = (
        <View className='qun-konu'>
          <Image className='qun-saoimg saoimgtop' mode='widthFix' src='https://minichat.g2ma.com/kedouimg/myorder71.png'></Image>
        </View>
      )
    }
    return (
    	<View className='page'>


	      <View className='qun-banner'>
          <View className='qun-saoma' onClick={this.saoma.bind(this)}>
            <Image className='qun-saoimg' mode='widthFix' src='https://minichat.g2ma.com/kedouimg/02.png'></Image>
          </View>
          <View className='qun-licon'>
            <View style={resy.length > 0 ? 'display:none' : 'display:block'}>
              {htmls}
            </View>
            {resy.map((item, index) => (
            <View className='qun-lists' onClick={this.getnav.bind(this,item.id,1)}>
              <View className='qun-toxnli'>
                <View className='qun-lfimg'>
                  <Image className='qun-userg' mode='scaleToFill' src={item.photo ? item.photo : 'https://minichat.g2ma.com/kedouimg/A6984900727459logo.png'}></Image>
                </View>
                <View className='qun-lfinfo'>
                  <View className='qun-inyi'>
                    <Text className='qun-botn'>{item.name}</Text>
                    <Text className='qun-gntn'>{item.note}</Text>
                  </View>
                  <View className='qun-loyan'>
                    <Image className='qun-userg1' mode='scaleToFill' src='https://minichat.g2ma.com/kedouimg/icon13.png' ></Image>
                    <Text>群聊成员（{item.clusterCount}）人</Text>
                  </View>
                </View>
              </View>
            </View>
            ))}
          </View>
	      </View>
				
      </View>
    )
  }
}
